#include<bits/stdc++.h>
#define int long long
using namespace std;
int t,n,m,a[100050],c[100050];
bool d[50][100050];
int winner[100050],winnerlen=0,winnerpower[100050];//trolla
int log2(int x)
{
	int l=0,r=25,ans=-1;
	while(l<=r)
	{
		int mid=(l+r)/2;
		if((1<<mid)>=x)
		{
			ans=mid;
			r=mid-1;
		}
		else
		{
			l=mid+1;
		}
	}
	return ans;
}
void trolla()
{
	int ans=0;
	for(int i=1;i<=m;i++)
	{
		winnerlen=c[i];
		for(int j=1;j<=c[i];j++)
		{
			winnerpower[j]=a[j];
			winner[i]=i;
		}
		while(winnerlen!=1)
		{
			for(int j=1;j<=winnerlen/2;j++)
			{
				if(winnerpower[i*2-1+d[i][j]]>=log2(c[i]/winnerlen)+1)
				{
					winner[i]=winner[i*2-1+d[i][j]];
					winnerpower[i]=winnerpower[i*2-1+d[i][j]];
				}
				else
				{
					winner[i]=winner[i*2-1+!d[i][j]];
					winnerpower[i]=winnerpower[i*2-1+!d[i][j]];
				}
			}
			winnerlen/=2;
		}
		ans^=(i*winner[1]);
	}
	cout<<ans;
	return;
}
signed main()
{
	
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	bool gooda=1;
	ios::sync_with_stdio(false);
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	for(int i=1;i<=m;i++)
	{
		cin>>c[i];
		if(gooda && (1<<log2(c[i]))!=c[i])
		{
			gooda=0;
		}
	}
	int k=log2(n);
	if((1<<k)<n)
	{
		k++;
	}
	for(int i=1;i<=k;i++)
	{
		for(int j=1;j<=(1<<(k-i));j++)
		{
			char ch;
			cin>>ch;
			d[i][j]=ch-'0';
		}
	}
	cin>>t;
	if(t!=1)
	{
		return 0;
	}
	int x[4];
	cin>>x[0]>>x[1]>>x[2]>>x[3];
	for(int i=1;i<=n;i++)
	{
		a[i]=a[i]^(x[i%4]);
	}
	if(gooda)
	{
		trolla();
		return 0;
	}
	int ans=0;
	for(int i=1;i<=m;i++)
	{
		winnerlen=log2(c[m]);
		if(1<<winnerlen<c[m])
		{
			winnerlen++;
		}
		winnerlen=1<<winnerlen;
		int winnerlen2=winnerlen;
		for(int i=1;i<=winnerlen;i++)
		{
			if(i<=c[m])
			{
				winner[i]=1<<i;
				winnerpower[i]=a[i];
			}
			else
			{
				winner[i]=(1<<(winnerlen+1))-1-(1<<c[m]);
				winnerpower[i]=-1;
			}
		}
		while(winnerlen!=1)
		{
			for(int i=1;i<=winnerlen/2;i++)
			{
				if(winnerpower[i*2-1+d[log2(winnerlen2/winnerlen)][i]]==-1)
				{
					winnerpower[i]=-1;
					winner[i]=winner[i*2]|winner[i*2-1];
				}
				else if(winnerpower[i*2-1+d[log2(winnerlen2/winnerlen)][i]]<log2(winnerlen2/winnerlen)+1)
				{
					winner[i]=winner[i*2-1+!d[log2(winnerlen2/winnerlen)][i]];
					winnerpower[i]=winnerpower[i*2-1+!d[log2(winnerlen2/winnerlen)][i]];
				}
				else
				{
					winner[i]=winner[i*2-1+d[log2(winnerlen2/winnerlen)][i]];
					winnerpower[i]=winnerpower[i*2-1+d[log2(winnerlen2/winnerlen)][i]];
				}
			}
		}
		int sum=0;
		int cnt=1;
		while(winner[1])
		{
			sum+=winner[1]>>(cnt-1)&1?cnt:0;
			cnt++;
			winner[1]>>=1;
		}
		ans=ans^(sum^i);
	}
	return 0;
}
